<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        div {
            background-color: hotpink;
            position: absolute;
            border-radius: 50%;
            width: 40px;
            height: 40px;
            top: 0;
            left: 0;
        }
    </style>
</head>

<body>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <script>
        // 1获取鼠标坐标 onmousemove
        // 2给盒子设置颜色和坐标 clientX clientY
        // 用定时器设置小球的移动 offsetWidth offsetHeight


        let box = document.querySelectorAll('div');

        document.onmousemove = function (e) {
            let left = e.clientX;
            let top = e.clientY;
            for (let i = 0; i < box.length; i++) {
                box[i].style.backgroundColor = 'rgb(' + sj(1, 255) + ',' + sj(1, 255) + ',' + sj(1, 255) + ')';

                setTimeout(function () {
                    box[i].style.left = left - box[i].offsetWidth / 2 + 'px';
                    box[i].style.top = top - box[i].offsetHeight / 2 + 'px';
                }, 50 * i)
            }

        }

        function sj(a, b) {
            return parseInt(Math.random() * (b - a) + a)
        }

    </script>
</body>

</html>